import 'package:common/base/base_lv_adapter.dart';
import 'package:common/base/base_lv_state.dart';
import 'package:common/base/base_stateful_widget.dart';
import 'package:flutter/material.dart';
import 'package:home/mvp/model/group_list_bean.dart';

import '../present/home_present.dart';

class GroupPage extends BaseStatefulWidget {

  ScrollController? scrollController;

  GroupPage({super.key,this.scrollController});

  @override
  State<GroupPage> createState() => _GroupPageState();
}

class _GroupPageState extends BaseLvState<GroupPage, Item> {
  late HomePresent _homePresent;

  @override
  void onStateInit() {
    super.onStateInit();
    _homePresent = HomePresent(this);
  }

  @override
  void onCreateScrollController() {
    if(null != widget.scrollController){
      scrollController = widget.scrollController!;
    }else {
      super.onCreateScrollController();
    }
  }

  @override
  bool onNeedAppBar() {
    return false;
  }

  //创建ListView的Item,并且绑定数据
  @override
  BaseLvAdapter<Item> onInitAdapter() {
    return BaseLvAdapter<Item>((Item t, int index) {
      return Container(
          margin: const EdgeInsets.fromLTRB(5, 0, 0, 0),
          child: Column(
              crossAxisAlignment: CrossAxisAlignment.start, children: createItemAndDataBind(t)));
    });
  }

  List<Widget> createItemAndDataBind(Item item) {
    List<Widget> widgets = [
      Text(item.title,
          textAlign: TextAlign.start,
          style: const TextStyle(color: Color(0xFFF29443), fontSize: 12))
    ];
    for (int index = 0; index < item.result.length; index++) {
      Widget secondaryWidget = Container(
        height: 80,
        width: double.infinity,
        margin: const EdgeInsets.fromLTRB(10, 10, 5, 0),
        decoration:
            BoxDecoration(color: const Color(0xFFFFF7F0), borderRadius: BorderRadius.circular(5)),
        child: Stack(
          alignment: Alignment.centerRight,
          children: <Widget>[
            const Positioned(
                left: 5,
                top: 0,
                child: Image(
                    width: 25,
                    height: 25,
                    alignment: Alignment.topRight,
                    image: AssetImage("assets/images/xxhdpi/free.png", package: "resource"),
                    fit: BoxFit.scaleDown)),
            const Positioned(
                right: 0,
                top: 0,
                child: Image(
                    width: 25,
                    height: 25,
                    alignment: Alignment.topRight,
                    image: AssetImage("assets/images/xxhdpi/red_leaf.png", package: "resource"),
                    fit: BoxFit.scaleDown)),
            const Positioned(
                right: 5,
                top: 0,
                child: Text(
                  "满",
                  textAlign: TextAlign.center,
                  style: TextStyle(color: Colors.white, fontSize: 12),
                )),
            const Positioned(
                child: Image(
                    width: 90,
                    height: 40,
                    alignment: Alignment.topRight,
                    image: AssetImage("assets/images/xxhdpi/exam_time_bg.png", package: "resource"),
                    fit: BoxFit.fill)),
            Positioned(
                child: Text(
                  item.result[index].beginTime,
              textAlign: TextAlign.center,
              style: const TextStyle(color: Color(0xFFF29443), fontSize: 12),
            )),
            const Positioned(
                left: 5,
                top: 20,
                child: Text(
                  "结构化考试",
                  textAlign: TextAlign.center,
                  style: TextStyle(color: Colors.black, fontSize: 12),
                )),
            Positioned(
              left: 5,
              bottom: 5,
              child: Wrap(
                children: [
                  const Image(
                      width: 25,
                      height: 25,
                      alignment: Alignment.center,
                      image: AssetImage("assets/images/xxhdpi/exam_classify_bg.png",
                          package: "resource"),
                      fit: BoxFit.scaleDown),
                  RichText(
                    text: TextSpan(
                        text: "${item.result[index].userAmount}人场 | 每人${item.result[index].questionAmount}题目 | ",
                        style: const TextStyle(color: Colors.black, fontSize: 12),
                        children: const [
                          TextSpan(
                            text: "5",
                            style: TextStyle(color: Colors.red, fontSize: 12),
                          ),
                          TextSpan(
                            text: "/0人报名",
                            style: TextStyle(color: Colors.black, fontSize: 12),
                          )
                        ]),
                  )
                ],
              ),
            ),
          ],
        ),
      );
      widgets.add(secondaryWidget);
    }
    return widgets;
  }

  @override
  void onLoadData() {
    showDelayLoadingDialog(context);
    Future.delayed(const Duration(seconds: 3), () {
      _homePresent.getGroupList();
    });
  }

  @override
  Future<void> onPullRefresh() async {
    super.onPullRefresh();
    await Future.delayed(const Duration(seconds: 3), () {});
  }

  @override
  Future<void> onPullLoadMore() async {
    super.onPullLoadMore();
    await Future.delayed(const Duration(seconds: 3), () {});

  }

  @override
  void onSuccess<D>(D d, String apiPath) {
    super.onSuccess(d, apiPath);
    if(d is GroupListBean){
      GroupListBean bean = d;
      refreshOrLoadSuccess(bean.items, total: bean.total);
    }
  }
}